import { defineConfig, UserConfig, ConfigEnv, loadEnv } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { resolve } from "path";
import { warppEnv } from "./src/utils/getEnv";
import viteCompression from "vite-plugin-compression";
// https://vite.dev/config/
export default defineConfig((mode: ConfigEnv): UserConfig => {
  const env = loadEnv(mode.mode, process.cwd())
  const viteEnv = warppEnv(env)
  return {
    plugins: [
      react(),
      viteEnv.VITE_USE_COMPRESS && viteCompression({
        verbose: true,
        disable: false,
        threshold: 10240,
        ext: '.gz',
        deleteOriginFile: false,
        filter: /\.(js|mjs|json|css |html)/i
      })
    ],
    resolve: {
      alias: {
        "@": resolve(__dirname, 'src')
      }
    },
    server: {
      host: '0.0.0.0',
      port: viteEnv.VITE_PORT,
      open: viteEnv.VITE_OPEN,
      proxy: {
        '/api': {
          // target:viteEnv.VITE_USE_API,
          // changeOrigin:true,
          // rewrite:path=>path.replace(/^\/api/,'')
        }
      }
    },
    build:{
      outDir:'dist',
      minify:'esbuild',
      rollupOptions:{
        output:{
          chunkFileNames:'assets/js/[name]-[hash].js',
          entryFileNames:'assets/js/[name]-[hash].js',
          assetFileNames:'assets/[ext]/[name]-[hash].[ext]'
        }
      }
    }
  }
}

)
